package com.itcast.bulls.stock.quote.grpc;


import com.itcast.bulls.stock.common.service.utils.BeanAssistant;
import com.itcast.bulls.stock.common.utils.GlobalConstants;
import com.itcast.bulls.stock.entity.quote.TradeStockKline;
import com.itcast.bulls.stock.quote.repository.trade.TradeStockKlineRepository;
import com.itcast.bulls.stock.struct.grpc.quote.GetKlineReponse;
import com.itcast.bulls.stock.struct.grpc.quote.GetKlineRequest;
import com.itcast.bulls.stock.struct.grpc.quote.KlineData;
import com.itcast.bulls.stock.struct.grpc.quote.StockKlineServiceGrpc;
import io.grpc.stub.StreamObserver;
import lombok.extern.log4j.Log4j2;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Log4j2
@GrpcService
public class GrpcStockKlineService extends StockKlineServiceGrpc.StockKlineServiceImplBase {

    /**
     * 行情K线的数据层接口
     */
    @Autowired
    private TradeStockKlineRepository tradeStockKlineRepository;

    /**
     * 获取股票K线数据接口
     * @param request
     * @param responseObserver
     */
    @Override
    public void getKline(GetKlineRequest request, StreamObserver<GetKlineReponse> responseObserver) {
        log.info("正在获取行情数据，请求为："+request);
        //1.根据股票代码以及时间范围，获取K线数据
        List<TradeStockKline> klineList = tradeStockKlineRepository
                .findAllByStockCodeAndTimeBetween(request.getStockCode(),
                        request.getTimeStart(), request.getTimeEnd());

        //2.将K线数据做遍历处理，转换为PB对象
        GetKlineReponse.Builder response = GetKlineReponse.newBuilder()
                .setStatus(GlobalConstants.STATUS_OK);
        if(null != klineList && !klineList.isEmpty()){
            klineList.stream().forEach(kline -> {
                //数据对象转换
                KlineData.Builder klineData = KlineData.newBuilder();
                //对象复制，排除null属性，避免复制出现报错（Long(null) -> long(不支持null))
                BeanAssistant.copyNotNullProps(kline,klineData);
                response.addKlineData(klineData.build());
            });
        }
        //3.将数据返回给grpc调用客户端
        responseObserver.onNext(response.build());
        responseObserver.onCompleted();
    }
}
